package com.hoshiibuy.purchase.controller;

import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.common.utils.GenerationCode;
import com.hoshiibuy.purchase.entity.Po;
import com.hoshiibuy.purchase.entity.Podetail;
import com.hoshiibuy.purchase.entity.Purchase;
import com.hoshiibuy.purchase.entity.param.PodetailAddParam;
import com.hoshiibuy.purchase.service.PurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 04:22 2019/5/16
 * @PACKAGENAME com.hoshiibuy.purchase.controller
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/purchase")
@Api(value = "采购", tags = {"采购单总管理"})
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;


    /**
     * @param poAddParam,podetailParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 实采单：采购单--销售订单自动生成  备货单--采购人员采购  新增
     */
    @PostMapping("/actualPurchase")
    @ApiOperation(value = "备货单/采购单")
    public Response actualPurchase(@RequestBody @Valid Purchase purchase) {
        try {
            //将PoAddParam与Po进行匹配
            Po data = BeanUtils.covertType(purchase.getPoAddParam(), Po.class);
            //将list中每一个PodetailAddParam与Podetail进行匹配，最后在组装成podetailList
            List<Podetail> podetailList = new ArrayList<>();
            for (PodetailAddParam podetailAddParam : purchase.getList()) {
                Podetail podetail = BeanUtils.covertType(podetailAddParam, Podetail.class);
                podetailList.add(podetail);
            }
            if (purchase.getPoAddParam().getType() == 9 || purchase.getPoAddParam().getType() == 1) {
                String code = GenerationCode.codeRule1();
                return Response.data(purchaseService.saveActualPurchase(code, data, podetailList));
            } else {
                throw new ServiceException(CommonError.error("新增失败,采购单类型有误"));
            }
        } catch (ServiceException e) {
            log.error("新增异常", e);
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("新增异常", e);
            return Response.failed();
        }
    }


    /**
     * @param poParam,podetailParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 补货单 新增
     */
/*    @PostMapping("/replenishment")
    @ApiOperation(value = "补货单")
    public Response replenishment(@RequestBody @Valid Po poParam, @RequestBody @Valid List<Podetail> list) {
        try {
            return Response.data(purchaseService.save(poParam, list));
        } catch (ServiceException e) {
            log.error("新增异常", e);
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("新增异常", e);
            return Response.failed();
        }
    }*/


    /**
     * @param poParam,podetailParam
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 预采单 新增
     */
/*    @PostMapping("/prePurchase")
    @ApiOperation(value = "实采单")
    public Response prePurchase(@RequestBody @Valid Po poParam, @RequestBody @Valid List<Podetail> list) {
        try {
            return Response.data(purchaseService.save(poParam, list));
        } catch (ServiceException e) {
            log.error("新增异常", e);
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("新增异常", e);
            return Response.failed();
        }
    }*/


}
